package com.ice.body.business.controller;

import com.github.pagehelper.PageHelper;
import com.ice.base.controller.BaseController;
import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import com.ice.body.business.model.WareHouseModel;
import com.ice.body.business.service.WareHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-06-22
 * Time: 03:05
 */
@RestController
@ApiIgnore(value = "业务接口-仓库")
public class WareHouseController extends BaseController{
    //---------------------------业务接口-仓库-----------------------start
    @Resource
    private WareHouseService wareHouseService;


    
    @ApiOperation(value = "业务接口-仓库-新增")
    @PostMapping("/api/WareHouse/insert")
    public AjaxResult<WareHouseModel> insert_WareHouse(WareHouseModel model) {
        AjaxResult r = ResponseResult.success();
        try{
            model.setCreateTime(new Date());
            wareHouseService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-仓库-删除")
    @PostMapping("/api/WareHouse/deleteById")
    public AjaxResult<WareHouseModel> deleteById_WareHouse(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            wareHouseService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-仓库-更新")
    @PostMapping("/api/WareHouse/updateById")
    public AjaxResult<WareHouseModel> update_WareHouse(WareHouseModel model) {
        AjaxResult r = ResponseResult.success();
        try{
            wareHouseService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-仓库-查询一个")
    @PostMapping("/api/WareHouse/selectById")
    public AjaxResult<WareHouseModel> selectById_WareHouse(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           WareHouseModel model = wareHouseService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-仓库-查询多个")
    @PostMapping("/api/WareHouse/selectAll")
    public AjaxResult<WareHouseModel> selectList_WareHouse(WareHouseModel model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<WareHouseModel> list = wareHouseService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-仓库-查询分页")
    @PostMapping("/api/WareHouse/selectListByPageHelper")
    public AjaxResult<WareHouseModel> selectListByPageHelper_WareHouse(@RequestParam(name = "pageSize") Integer pageSize,
                                                                       @RequestParam(name = "pageNum") Integer pageNum,
                                                                       WareHouseModel model,
                                                                       String keyWord){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           if(StringUtils.isEmpty(keyWord)) {
               list = wareHouseService.selectListByModel(model);
           } else {
               list = wareHouseService.selectListByKeyWord(keyWord);
           }
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------业务接口-仓库-----------------------end





}
